//
//  NewsViewController.m
//  WangYiNews
//
//  Created by lanou3g on 15/10/10.
//  Copyright (c) 2015年 zhengming. All rights reserved.
//

#import "NewsViewController.h"
#import "HeadlineNewsViewController.h"

enum titleName {
    toutiao = 200,
    yule,
    redian,
    tiyu,
    beijing,
    caijing,
    keji
//    tupian,
//    genti,
//    zhibo,
//    shishang,
//    qingsongyike,
//    qiche
};

@interface NewsViewController ()

@property (nonatomic, strong) UIView *lineView;

@property (nonatomic, strong) UIButton *lastBtn; // 记录上一次点击的button
@end

@implementation NewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO; // 设置内距
    self.navigationController.navigationBar.hidden = YES;
    [self subView];
    NSLog(@"adf");
    NSLog(@"adsf");
    NSLog(@"adsf");

    NSLog(@"qer323r32");
    NSLog(@"asdf");
    
    // alsjfd;lkaj;lkfja;lsk

    /*
    @property (weak, nonatomic) IBOutlet UIScrollView *scrollView;@property (weak, nonatomic) IBOutlet UIButton *beHereBtn;@end@implementation ViewController- (void)viewDidLoad {	//确定scrollView能滚动多大范围	self.scrollView.contentSize=CGSizeMake(320,CGRectGetMaxY(self.beHereBtn.frame)+10);	//在scrollView周边增加滚动区域	self.scrollView.contentInset=UIEdgeInsetsMake(64, 0, 49, 0);	//设置scrollView里的控件的移动位置	self.scrollView.contentOffset=CGPointMake(0, -64);	[super viewDidLoad];	// Do any additional setup after loading the view, typically from a nib.}@end
    */
    
//    self.contentViewOrign = -kScreenWidth;
//    CGRect frame = headlineNewsVC.tableView.frame;
//    frame.origin = CGPointMake(kScreenWidth, 0);
//    headlineNewsVC.tableView.frame = frame;
    
    [self.view layoutIfNeeded]; // 手动让约束提前执行。

    HeadlineNewsViewController *headlineNewsVC = [[HeadlineNewsViewController alloc] init];
    
    [self addChildViewController:headlineNewsVC];
    headlineNewsVC.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    
    NSLog(@"headlineNewsVC.tableView = %@, headlineNewsVC.view = %@", headlineNewsVC.tableView , headlineNewsVC.view);
    
    [self.contentView addSubview:headlineNewsVC.view];
   
    
}

- (void)viewDidAppear:(BOOL)animated
{
    HeadlineNewsViewController *headlineNewsVC = [[HeadlineNewsViewController alloc] init];
    
    [self addChildViewController:headlineNewsVC];
    headlineNewsVC.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    
    NSLog(@"headlineNewsVC.tableView = %@, headlineNewsVC.view = %@", headlineNewsVC.tableView , headlineNewsVC.view);
    
    [self.contentView addSubview:headlineNewsVC.view];
}

- (void)subView
{
    self.newsBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
    [self.newsBar setBarTintColor:[UIColor redColor]];
    [self.view addSubview:self.newsBar];
    
    UIImageView *headImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 70, 30)];
    [headImage setBackgroundColor:[UIColor yellowColor]];
    [self.newsBar addSubview:headImage];
    
    self.todayBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.todayBtn.frame = CGRectMake(kScreenWidth - 80, 20, 30, 30);
    [self.todayBtn setBackgroundColor:[UIColor yellowColor]];
    [self.newsBar addSubview:self.todayBtn];
    
    self.otherBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.otherBtn.frame = CGRectMake(kScreenWidth - 40, 20, 30, 30);
    [self.otherBtn setBackgroundColor:[UIColor yellowColor]];
    [self.newsBar addSubview:self.otherBtn];
    
    
    NSArray *titleName = @[@"头条",
                           @"娱乐",
                           @"热点",
                           @"体育",
                           @"北京",
                           @"财经",
                           @"科技"];
//                           @"图片",
//                           @"跟帖",
//                           @"直播",
//                           @"时尚",
//                           @"轻松一刻",
//                           @"汽车",
//                           @"段子",
//                           @"军事",
//                           @"房产",
//                           @"历史",
//                           @"家居",
//                           @"原创",
//                           @"画报",
//                           @"游戏",
//                           @"健康",
//                           @"政务"];
    
    NSInteger width = 0;
    for (int i = 0; i < titleName.count; i++) {
        NSString *title = titleName[i];
        // 计算文本所占长度
        CGSize buttonSize = [title sizeWithAttributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName]];
        
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        button.frame = CGRectMake(width + 25, 0, buttonSize.width, 30);
        width += buttonSize.width + 25;
        button.tag = 200 + i;
        [button setTitle:title forState:(UIControlStateNormal)];
        [button setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
        button.alpha = 0.7;
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button addTarget:self action:@selector(titleBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.titleContentView addSubview:button];
        
        if ([title isEqualToString:@"头条"]) {
            [button setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
            self.lastBtn = button;
            button.alpha = 1;
        }
    }
    // 设置scrollView的滑动距离
    self.titleViewWidth.constant = width - kScreenWidth + 20;
    
    CGSize firstTitleSize = [@"头条" sizeWithAttributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName]];
    self.lineView = [[UIView alloc] initWithFrame:CGRectMake(20, 28, firstTitleSize.width + 10, 2)];
    self.lineView.backgroundColor = [UIColor redColor];
    [self.titleContentView addSubview:self.lineView];
}

#pragma mark -按钮触发方法
- (void)titleBtnAction:(UIButton *)sender
{
    NSString *title = [sender titleForState:UIControlStateNormal];
     CGSize titleSize = [title sizeWithAttributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName]];
    self.lineView.frame = CGRectMake(sender.frame.origin.x - 5, 28, titleSize.width + 10, 2);
    [sender setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
    sender.alpha = 1;
    [self.lastBtn setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    self.lastBtn.alpha = 0.7;
    self.lastBtn = sender;
    
    switch (sender.tag) {
        case toutiao:
            
            break;
        case yule:
            
            break;
        case redian:
            
            break;
        case tiyu:
            
            break;
        case beijing:
            
            break;
            
            
        default:
            break;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
